package com.lz.yz.order.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.order.entity.Orders;
import com.lz.yz.order.model.OrderModel;
import com.lz.yz.order.service.OrdersService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* Created by CodeGenerator on 2017/11/09.
*/
@RestController
@RequestMapping("/orders")
public class OrdersController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    OrdersService ordersService;

    @Autowired
    HttpServletRequest request;

    @ApiOperation(value="用户下单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(name = "orderModel", value = "订单实体类", dataType = "OrderModel", paramType = "body"),
    })
    @PostMapping("/order")
    public ReturnValue add(@RequestBody OrderModel orderModel) {
        try {
            Integer accountId = (Integer) request.getAttribute("accountId");
            return ordersService.add(accountId,orderModel);
        } catch (Exception e) {
            logger.error("新增订单出错" + orderModel, e);
            return ReturnValue.generateFalseReturnValue("下单失败");
        }
    }


}
